package com.wakala.employment.fragment.subscribe;

import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.cy.tablayoutniubility.FragPageAdapterVp2;
import com.cy.tablayoutniubility.TabAdapter;
import com.cy.tablayoutniubility.TabLayoutScroll;
import com.cy.tablayoutniubility.TabMediatorVp2;
import com.cy.tablayoutniubility.TabViewHolder;
import com.flyco.tablayout.SlidingTabLayout;
import com.stx.xmarqueeview.XMarqueeView;
import com.wakala.employment.R;
import com.wakala.employment.adapter.marquee.MarqueeViewAdapter;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentSubscribeJobBinding;
import com.wakala.employment.fragment.circle.CircleRecommendFragment;
import com.wakala.employment.fragment.news.NewsFragment;
import com.wakala.employment.fragment.other.SimpleKindFragment;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 订阅工作
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class SubscribeJobFragment extends BaseFragment<FragmentSubscribeJobBinding> implements View.OnClickListener {

    private  SlidingTabLayout tabNavTab = null;
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private MyPagerAdapter mAdapter;
    private final String[] mTitles = {
            "广场", "资源", "活动", "名片"
    };

    @NonNull
    @Override
    protected FragmentSubscribeJobBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentSubscribeJobBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected void initViews() {
       /* for (String title : mTitles) {
            mFragments.add(SimpleKindFragment.getInstance(title));
        }
        View decorView = getActivity().getWindow().getDecorView();
        ViewPager vp = findViewById(R.id.vp);
        mAdapter = new MyPagerAdapter(getFragmentManager());
        vp.setAdapter(mAdapter);
        tabNavTab = binding.tlNavTab;
        tabNavTab.setViewPager(vp,mTitles);
        tabNavTab.setTabPadding(5);
        tabNavTab.setHorizontalScrollBarEnabled(true);*/
        ViewPager2 viewPager2 = findViewById(R.id.view_pager);viewPager2.setUserInputEnabled(false);
        TabLayoutScroll tabLayoutLine = findViewById(R.id.tablayout);
//        tabLayoutLine.setSpace_horizontal(dpAdapt(20)).setSpace_vertical(dpAdapt(8));
        FragPageAdapterVp2<String> fragmentPageAdapter = new FragPageAdapterVp2<String>(this) {

            @Override
            public Fragment createFragment(String bean, int position) {
                if(position==0){
                    return new NewsFragment(601);
                }else{
                    return SimpleKindFragment.getInstance( getList_bean().get(position));
                }

            }

            @Override
            public void bindDataToTab(TabViewHolder holder, int position, String bean, boolean isSelected) {
                TextView textView = holder.getView(R.id.tv);
                if (isSelected) {
                    textView.setTextColor(0xffe45540);
                    //textView.setTextSize(18);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                } else {
                    textView.setTextColor(0xff444444);
                    //textView.setTextSize(16);
                    //textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                }
                textView.setText(bean);
            }

            @Override
            public int getTabLayoutID(int position, String bean) {
                return R.layout.item_tab;
            }
        };
        TabAdapter<String> tabAdapter = new TabMediatorVp2<String>(tabLayoutLine, viewPager2).setAdapter(fragmentPageAdapter);

        List<String> list = new ArrayList<>();
        list.add("推荐");
        list.add("广场");
        list.add("资源");
        list.add("活动");
        list.add("知识");
        list.add("名片");
        //list.add("漫画");
        fragmentPageAdapter.add(list);
        tabAdapter.add(list);
    }

    @Override
    protected com.xuexiang.xpage.utils.TitleBar initTitleBar() {
        return super.initTitleBar();
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
      /*  List<String> data = new ArrayList<>();
        data.add("免费加入本地招人找活群");
        data.add("附件找人找活信息推送");
        data.add("优质创业项目推荐");
        data.add("超多赚钱机会分享");

        XMarqueeView xMarqueeView = binding.marquee2;
        try {
            xMarqueeView.setAdapter(new MarqueeViewAdapter(data, getContext()));
        } catch (Exception e) {
            //catchsetAdapter重复设置adapter抛出异常的异常。
            Log.w("", e.getMessage());
        }*/
        return null;
    }


    @Override
    public void onClick(View view) {

    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}